package com.gxuwz.prrs.service.impl;

import com.google.gson.reflect.TypeToken;
import com.gxuwz.prrs.bean.AppointmentRecord;
import com.gxuwz.prrs.bean.CommonResponse;
import com.gxuwz.prrs.bean.Page;
import com.gxuwz.prrs.service.BaseService;
import com.gxuwz.prrs.utils.RequestUtils;
import okhttp3.FormBody;

import java.lang.reflect.Type;

/**
 * @author 张钧
 * @Description
 * @create 2023-05-27 下午 07:15
 */
public class AppointmentRecordServiceImpl implements BaseService<AppointmentRecord, Page<AppointmentRecord>> {
	Type type = new TypeToken<CommonResponse<Page<AppointmentRecord>>>() {
	}.getType();

	@Override
	public Page<AppointmentRecord> page(Page<AppointmentRecord> page) {
		String url = "/appointment/page?size=" + page.getSize() + "&current=" + page.getCurrent();
		CommonResponse<Page<AppointmentRecord>> response = RequestUtils.getRequest(url, type);
		if (response != null) {
			return response.getData();
		}
		return null;
	}

	public Boolean updateAppointment(AppointmentRecord appointmentRecord) {
		Long appointmentRecordId = appointmentRecord.getAppointmentRecordId();
		String checkInTime = appointmentRecord.getCheckInTime();
		String checkOutTime = appointmentRecord.getCheckOutTime();
		String status = appointmentRecord.getStatus();
		FormBody.Builder builder = new FormBody.Builder();
		if (appointmentRecordId != null) {
			builder.add("account", String.valueOf(appointmentRecordId));
			if (checkInTime != null) {
				builder.add("checkInTime", checkInTime);
			}
			if (checkOutTime != null) {
				builder.add("checkOutTime", checkOutTime);
			}
			if (status != null) {
				builder.add("status", status);
			}
			String url = "/appointment";
			CommonResponse<Boolean> response = RequestUtils.putRequest(url, builder.build(), new TypeToken<CommonResponse<Boolean>>() {
			}.getType());
			if (response != null) {
				return response.getData();
			}
		}
		return null;
	}

	public AppointmentRecord getUpcomingAppointment() {
		String url = "/appointment/upcoming";
		CommonResponse<AppointmentRecord> response = RequestUtils.getRequest(url, new TypeToken<CommonResponse<AppointmentRecord>>() {
		}.getType());
		if (response != null) {
			return response.getData();
		}
		return null;
	}
}
